package com.ljx.offer;

import org.junit.Test;

import java.util.LinkedList;
import java.util.List;

/**
 * @author 李捷禧
 * Date: 2023/3/30
 * ClassName: C_45
 */

public class C_45 {
    /**
     * 先比价排序，再拼接起来
     * @param nums
     * @return
     */
    public String minNumber(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i+1; j < nums.length; j++) {
                if(isChange(nums[i],nums[j]) || nums[j] == 0){
                    int t = nums[i];
                    nums[i] = nums[j];
                    nums[j] = t;
                }
            }
        }

        StringBuffer sb = new StringBuffer();
        for (Integer num : nums) {
            sb.append(num);
        }

        return sb.toString();
    }

    /**
     * 判断ab > ba
     * 如果是就要交换a ，b位置
     * @param a
     * @param b
     * @return
     */
   Boolean isChange(int a,int b){
       int c = 0;
       int d = 0;
       int p = a;
       int q = b;
       while(p != 0){
           p /=10;
           c++;
       }while (q !=0){
           q /=10;
           d++;
       }
       if((b*(Math.pow(10,c)) + a) < (a*(Math.pow(10,d)) + b)) {
          return true;
       }else return false;
   }

   @Test
    public void test(){
       int[] nums = new int[]{1,2,3,4,5,6,7,8,9,0};
       System.out.println(minNumber(nums));
   }

}
